package com.design.memento.demo2;


import java.util.ArrayList;
import java.util.List;

/**
 * @author xiemiao
 * @description
 * @date 2023/08/18
 */
public class Originator {
    private String state;

    private List<Memento> mementos = new ArrayList<>();

    public Originator() {
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
        mementos.add(new Memento(state));
    }

    public void restoreStateFromMemento(int index) {
        if (index >= 0 && index < mementos.size()) {
            state = mementos.get(index).getState();
        }
    }

    private static class Memento {
        private String state;

        public Memento(String state) {
            this.state = state;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }
    }
}
